package com.linwen.excise.od.C;

import java.util.Arrays;

/**
 * @ClassName _堆内存申请
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/5 12:20 PM
 * @Version 1.0
 */
public class _堆内存申请 {
    public static void main(String[] args) {
        int m = 1;
        int[][] arr = new int[][]{{0,1},{3,2}};
        System.out.println(function(m,arr));

    }

    private static int function(int m, int[][] arr) {
        if(m>100){
            return -1;
        }
        Arrays.sort(arr, (a,b)->a[0]-b[0]);
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i][1]>arr[i+1][0]){// 检察合法性
                return -1;
            }
        }
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i+1][0]-arr[i][1]>=m){
                return arr[i][1];
            }
        }
        if(99-arr[arr.length-1][1]>=m){
            return arr[arr.length-1][1];
        }
        return -1;
    }
}
